Date.prototype.format = function (fmt) {
	var o = {
		"M+": this.getMonth() + 1,                 //月份 
		"d+": this.getDate(),                    //日 
		"h+": this.getHours(),                   //小时 
		"m+": this.getMinutes(),                 //分 
		"s+": this.getSeconds(),                 //秒 
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度 
		"S": this.getMilliseconds()             //毫秒 
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return fmt;
}

//编译日志
function AddBuildLog(txt) {
	$("#txtBuildLog").val($(new Date().format("yyyy-MM-dd hh:mm:ss")+" : "+"#txtBuildLog").val() + txt + "\r\n")
}
//操作日志
function AddActionLog(txt) {
	txt=new Date().format("yyyy-MM-dd hh:mm:ss")+" "+txt;
	var ta=$("#txtOutput");
	ta.val(ta.val() + txt + "\r\n");
	
	ta[0].scrollTop = ta[0].scrollHeight;   // 内容滚动到最后一行
}


function AddLog(isRead, title, Id) {
	var color = isRead ? "info" : "warning";
	var strVar = "<div class=\"bs-callout bs-callout-" + color + "\" title=\"" + title + "\" > <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;<\/span><\/button><strong>策略名<\/strong> " + title.substring(0, 12) + " ...<\/div>";
	var ele = $(strVar);
	$("span", ele).click(function () {
		$(this).parent().parent().remove();
	});
	ele.click(function () {
		alert("id")
	});

	$("#logs").append(ele);
}

 function startProcess(id) {

        var schema = $('#treeSchema').jstree(true).get_selected();
        if (schema.length < 1) {
            alert("请选个回测场景");
            return;
        }
        reqRunStrategy(id, schema[0]);
    }
function initCodeMirrorEditor(id) {
	var htmlElement = document.getElementById(id);

	var editor = CodeMirror.fromTextArea(htmlElement, {
		lineNumbers: true,
		matchBrackets: true,
		extraKeys: {
			// "Ctrl-Space": "autocomplete", TODO: to get params inside parentheses
			"'.'": "dotautocomplete",
			'Ctrl-S': function () {
				SaveCode(editor.getWrapperElement().parentElement.id.replace('editor', '')+".py", editor.getValue());
				//alert(editor.getValue())
			},
			'Ctrl-R': function (id) {
				SaveCode(editor.getWrapperElement().parentElement.id.replace('editor', '')+".py", editor.getValue());			
				var strategy=editor.getWrapperElement().parentElement.id.replace('editor', '');
				startProcess(strategy);
			},
			'Alt-R': function (id) {
				SaveCode(editor.getWrapperElement().parentElement.id.replace('editor', '')+".py", editor.getValue());
				var strategy=editor.getWrapperElement().parentElement.id.replace('editor', '');
				startProcess(strategy);
			}
		},
		mode: "text/x-python",
		indentUnit: 4,
		//foldGutter: true,
		gutters: ["CodeMirror-lint-markers"]
		//gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
	});

	editor.refresh();
	editor.display.wrapper.style.fontSize = '13px';
	editor.display.wrapper.style.fontFamily = 'Monaco, Menlo, "Ubuntu Mono", Consolas, source-code-pro, monospace';
	editor.setSize('100%', '100%');

	//CodeMirror.registerHelper("hint", "IntelliSense", IntelliSenseHandler);
	CodeMirror.commands.dotautocomplete = function (ed) {
		var cur = ed.getCursor();
		ed.replaceSelection(ed.getSelection() + ".");
		ed.setCursor({
			line: cur.line,
			ch: cur.ch + 1
		});

		CodeMirror.showHint(ed, CodeMirror.hint.IntelliSense, {
			async: true
		});
	}

	return editor;
}


